//
//  DINGForwardStatusCell.swift
//  0730Microblog
//
//  Created by ding on 15/8/9.
//  Copyright © 2015年 ding. All rights reserved.
//

import UIKit

class DINGForwardStatusCell: DINGStatusCell {
    
    override var statuses: DINGMicroblogStatus? {
        didSet {
//            if let retweeted_status = statuses?.retweeted_status {
                let textContent = statuses?.retweeted_status?.text ?? ""
                let statusAuthor = statuses?.retweeted_status?.user?.name ?? ""
                forwardLabel.text = "@" + statusAuthor + ":" + textContent
                forwardLabel.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 16
                
                forwardLabel.numberOfLines = 0
//            }
        }
    }
    
    override func prepareHomeCellUI() {
        super.prepareHomeCellUI()

        contentView.insertSubview(backgroundBtn, belowSubview: pictureView)
        contentView.insertSubview(forwardLabel, aboveSubview: backgroundBtn)
        
        
//        forwardLabel.text = "Youth is not a time of life; it is a state of mind; it is not a matter of rosy cheeks, red lips and supple knees; it is a matter of the will, a quality of the imagination, a vigor of the emotions; it is the freshness of the deep springs of life. "
        
        // 设置布局
        
        backgroundBtn.translatesAutoresizingMaskIntoConstraints = false
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[targetView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["targetView" : backgroundBtn]))
        contentView.addConstraint(NSLayoutConstraint(item: backgroundBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 8))
        contentView.addConstraint(NSLayoutConstraint(item: backgroundBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bottomView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: -8))
        contentView.addConstraint(NSLayoutConstraint(item: backgroundBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        
        forwardLabel.translatesAutoresizingMaskIntoConstraints = false
        contentView.addConstraint(NSLayoutConstraint(item: forwardLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: backgroundBtn, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 4))
        contentView.addConstraint(NSLayoutConstraint(item: forwardLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: backgroundBtn, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: forwardLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: backgroundBtn, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -16))
        
        //MARK: - 图片视图的约束
        pictureView.translatesAutoresizingMaskIntoConstraints = false
        //        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[targetView]-10-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["targetView" : pictureView]))
        //        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[topView]-10-[targetView]-10-[bottomView]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["topView": contentLabel, "targetView" : pictureView, "bottomView" : bottomView]))
        contentView.addConstraint(NSLayoutConstraint(item: pictureView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        pictureWidthConstraint = contentView.constraints.last
        contentView.addConstraint(NSLayoutConstraint(item: pictureView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: forwardLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 8))
        pictureTopConstraint = contentView.constraints.last
        contentView.addConstraint(NSLayoutConstraint(item: pictureView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bottomView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: -8))
        contentView.addConstraint(NSLayoutConstraint(item: pictureView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 8))
        contentView.addConstraint(NSLayoutConstraint(item: pictureView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        pictureHeightConstraint = contentView.constraints.last

    }
    
    //MARK: - 懒加载控件
    private lazy var backgroundBtn: UIButton = {
       let btn = UIButton()
        btn.backgroundColor = UIColor.yellowColor()
        
        return btn
    }()
    private lazy var forwardLabel: UILabel = UILabel(tColor: UIColor.lightGrayColor(), fontSize: 14)
}
